package es.ats.codington.festival.daos;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import es.ats.codington.festival.domain.Event;
import es.ats.codington.festival.domain.Place;
import es.ats.codington.festival.exceptions.DAOSExceptions;

import es.ats.codington.festival.interfaces.daos.iEventDAO;
import es.ats.codington.festival.utils.FERSDataConnection.FersDC;
import es.ats.codington.festival.utils.Query.JSonQuery;

public class EventDAOImpl implements iEventDAO {

	private Connection connection = null;
	private PreparedStatement statement = null;
	private ResultSet resultSet=null;
	//JSonQuery json = new JSonQuery();
	
	public EventDAOImpl(Connection connection) {
		super();
		this.connection = connection;
	}


	/**
	 * Constructor 
	 * @param connection
	 * @param statement
	 * @param resultSet
	 */
	public EventDAOImpl() {
		super();
	}

	
	/**
	 * @param event1
	 * @return
	 * @throws SQLException 
	 */
	
	@Override
	public Event viewEvent(int iDevent) throws Exception{
		
			
		statement = connection.prepareStatement("SELECT * from event WHERE idevent=?");
		statement.setInt(1,iDevent);
		resultSet = statement.executeQuery();
		Event event = new Event();
		Place place2 = new Place();
		int idit = 0;
			
		while (resultSet.next()) {
					
			event.setNamevent(resultSet.getString("name"));
			event.setPrice(resultSet.getFloat("price"));
			event.setDescription(resultSet.getString("descriptions"));
			event.setiDevent(iDevent);
			
			

			idit = resultSet.getInt("idplace"); 
			PreparedStatement statement2 = connection.prepareStatement("SELECT * from place WHERE idplace=?");
			statement2.setInt(1, idit);
			ResultSet resultSet2 = statement2.executeQuery();
			while(resultSet2.next()){
				place2.setName(resultSet2.getString("name"));
				place2.setAddress(resultSet2.getString("address"));
				place2.setCapacity(resultSet2.getInt("capacity"));
				place2.setTown(resultSet2.getString("town"));
				place2.setDescription(resultSet2.getString("description"));
				place2.setiDplace(idit);
							
			}
			event.setPlace(place2);
			event.setSeats(resultSet.getInt("seats"));
			resultSet2.close();
		}
		resultSet.close();
		return event;

	}

	@Override
	public ArrayList<Event> viewAllEvents() throws Exception{
		
		  	connection = FersDC.getConnection();
			statement = connection.prepareStatement("SELECT * from event");
			resultSet = statement.executeQuery();
			ArrayList<Event> eventList = new ArrayList <Event>();
			Place place = new Place();
			int idit = 0;
		while (resultSet.next()) {
			Event event = new Event();
		
			
			event.setNamevent(resultSet.getString("name"));
			event.setPrice(resultSet.getFloat("price"));
			event.setDescription(resultSet.getString("descriptions"));
			event.setSeats(resultSet.getInt("seats"));
			event.setiDevent(resultSet.getInt("idevent"));
			
			Place place2 = new Place();
			idit = resultSet.getInt("idplace"); 
			PreparedStatement statement2 = connection.prepareStatement("SELECT * from place WHERE idplace=?");
			statement2.setInt(1, idit);
			ResultSet resultSet2 = statement2.executeQuery();
			while(resultSet2.next()){
				place2.setName(resultSet2.getString("name"));
				place2.setAddress(resultSet2.getString("address"));
				place2.setCapacity(resultSet2.getInt("capacity"));
				place2.setTown(resultSet2.getString("town"));
				place2.setDescription(resultSet2.getString("description"));
				place2.setiDplace(idit);
							
			}
			resultSet2.close();
			event.setPlace(place2);
			eventList.add(event);
			
		}
				
		resultSet.close();
		return eventList;

	}
	
	@Override
	public ArrayList<Integer> viewMyEvents(int idvisitor) throws Exception {
		
	try{
		statement = connection.prepareStatement("SELECT `idevent` FROM `visitor-places` WHERE idvisitor = ?");
		statement.setInt(1, idvisitor);
		resultSet = statement.executeQuery();
				
		ArrayList<Integer> idMyEvents= new ArrayList<Integer>();
			int iter=0;
			while (resultSet.next()) {
				idMyEvents.add(resultSet.getInt("idevent"));
				iter++;						
				}
	
					
		resultSet.close();
		return idMyEvents;

		}catch (Exception ee) {
			System.out.println(" registerDAO 1: "+ ee.getMessage());			
			return null;
		}
	
	}
	
	public ArrayList<Integer> getIdsEvent(int idplace) throws Exception {

		ArrayList<Integer> idsEventByType= new ArrayList<Integer>();
		try{
			
							
			// Now making changes			
			PreparedStatement myEvent = connection.prepareStatement("SELECT idevent FROM event WHERE idplace= ? ");
			myEvent.setInt(1, idplace);
			
			ResultSet ids = myEvent.executeQuery();
			
			int iter=0;
			while (ids.next()) {
				idsEventByType.add(ids.getInt("idevent"));
				iter++;						
				}
	
					
		ids.close();
		return idsEventByType;
			
		
		}catch (Exception ex){
			System.out.println("Error Exception con mensaje: " + ex.getMessage());
			connection.rollback();
			
		}
		return idsEventByType;
		
		
	}


		
}
	



		